package com;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.util.JsonUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

@WebServlet("/SevletFileUpLoad")
public class SevletFileUpLoad1 extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        FileItemFactory fileItemFactory = new DiskFileItemFactory();
        ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
        JsonUtil jsonUtil = new JsonUtil();
        try {
            List<FileItem> fileItems = servletFileUpload.parseRequest(req);
            String name="";
            for (FileItem fileItem : fileItems) {
                if (!fileItem.isFormField()){
                    //tomcat项目运行磁盘路径
                    String realPath = req.getServletContext().getRealPath("/img");
                    File file = new File(realPath);
                    if (!file.exists()){
                        file.mkdirs();
                    }
                    name = fileItem.getName();
                    InputStream inputStream = fileItem.getInputStream();
                    FileOutputStream fileOutputStream = new FileOutputStream(new File(file, name));
                    byte[] byf=new byte[1024];
                    int len=0;
                    while((len=inputStream.read(byf))!=-1){
                        fileOutputStream.write(byf,0,len);
                    }
                    fileOutputStream.close();
                    inputStream.close();

                }else {

                }
                int Port = req.getLocalPort();
                String ip = req.getLocalAddr();
                String contextPath = req.getContextPath();
                String path="http://"+ip+":"+Port+"/"+contextPath+"/img/"+name;
                System.out.println("路径:"+path);
                jsonUtil.setCode(0);
                jsonUtil.setMsg("成功");
                jsonUtil.setData(path);
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("上传失败:"+e.getMessage());
        }
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        String s = gson.toJson(jsonUtil);
        writer.print(s);
//        SevletFileUpLoad sevletFileUpLoad = new SevletFileUpLoad(FileItemFactory);
    }

}
